// 千分位，默认保留2位小数
export function parseToThousandth(num, point = 2) {
  if (Number.isNaN(parseInt(num, 10))) return 0;
  let [sInt, sFloat] = (Number.isInteger(num)
    ? `${num}`
    : num.toFixed(point)
  ).split('.');
  sInt = sInt.replace(/\d(?=(\d{3})+$)/g, '$&,');
  return sFloat ? `${sInt}.${sFloat}` : `${sInt}`;
}

// 金额千分位，默认最小保留2位小数
export function parseToThousandthMoney(num, point = 2) {
  if (Number.isNaN(parseInt(num, 10))) return 0;
  let [sInt, sFloat] = (Number.isInteger(num)
    ? `${num}.00`
    : num.toFixed(point)
  ).split('.');
  sInt = sInt.replace(/\d(?=(\d{3})+$)/g, '$&,');
  return sFloat ? `${sInt}.${sFloat}` : `${sInt}`;
}

// 将数字转换成万 保留两位小数
export const parseToTenThousand = num => {
  let result = num / 10000;
  result = Math.floor(result * 100) / 100;
  let s_x = result.toString(); // 将数字转换为字符串
  let pos_decimal = s_x.indexOf('.'); // 小数点的索引值
  // 当整数时，pos_decimal=-1 自动补0
  if (pos_decimal < 0) {
    pos_decimal = s_x.length;
    s_x += '.';
  }
  // 当数字的长度< 小数点索引+2时，补0
  while (s_x.length <= pos_decimal + 2) {
    s_x += '0';
  }
  return s_x;
};

// 小数数字转换为百分比格式 保留两位小数 etc: 0.1667 => '16.67%'
export const parseToPercent = point => {
  let str = Number(point * 100).toFixed(1);
  str += '%';
  return str;
};

// 百分比格式转换为小数数字  etc: '16.67%' => 0.1667
export const parseToPoint = percent => {
  let str = percent.replace('%', '');
  str = str / 100;
  return str;
};
